
#include <iostream>
#include <memory>
#include <string>
#include <unistd.h>
#include "TcpServer.hpp"
#include "HttpProtocol.hpp"

std::string HandlerHttpRequest(std::string &request)
{
    HttpRequest req;
    req.Deserialize(request);
    req.DebugHttp();

    std::string content="<html><h1>hello bite</h1></html>";
    std::string httpstauusline="Http/1.0 200 OK\r\n";
    std::string httpheader = "Content-Length: " + std::to_string(content.size()) + "\r\n";
    httpheader += "\r\n";

    std::string httpresponse=httpstauusline+httpheader+content;
    return httpresponse;
}


void Usage(char *argv[])
{
    std::cout << "Usage : " << argv[0] << " port" << std::endl;
}
int main(int argc,char*argv[])
{
    if(argc!=2)
    {
        Usage(argv);
        return 0; 
    }

    uint16_t port=std::stoi(argv[1]);
    std::unique_ptr<TcpServer> tsvr(new TcpServer(port,HandlerHttpRequest));
    std::cout<<"开始执行\n";
    tsvr->loop();
    return 0;
}  